//
//  HTImageViewController.m
//  HeartTravel
//
//  Created by lanou on 15/11/2.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "HTImageViewController.h"
#import "PKRequestManager.h"
#import "HTImageModel.h"
#import "HTImageCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "MJPhoto.h"
#import "MJPhotoBrowser.h"

@interface HTImageViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *mainCollectionView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@end

@implementation HTImageViewController
- (NSMutableArray *)dataSourceArray{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}

- (void)requestDataSource{
    NSString *urlString = [@"http://api.breadtrip.com//destination/place" stringByAppendingFormat:@"/%@/%@/photos/?gallery_mode=1&count=36&sign=5302361023ac5b508d6ec1deeaa07aff", self.model.type, self.model.iId];
    [PKRequestManager requestWithType:GET urlString:urlString parDic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data
 options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *dic in dict[@"items"]) {
            HTImageModel *model = [[HTImageModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataSourceArray addObject:model];
        }
        [self.mainCollectionView reloadData];
    } error:^(NSError *error) {
        NSLog(@"照片墙数据刷新失败%@", error);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestDataSource];
    
    self.topView.backgroundColor = [UIColor colorWithRed:0.351 green:0.359 blue:0.350 alpha:1.000];
    [self.leftButton setBackgroundImage:[UIImage imageNamed:@"返回"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(didClickBackLastViewController) forControlEvents:(UIControlEventTouchUpInside)];
    [self addCollectionView];
}

- (void)addCollectionView{
    CGFloat width = (SCREEN_WIDTH - 6)/3;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(width, width);
    layout.minimumInteritemSpacing = 3;
    layout.minimumLineSpacing = 3;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 3, 0);
    _mainCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT - 70) collectionViewLayout:layout];
    _mainCollectionView.backgroundColor = [UIColor clearColor];
    _mainCollectionView.dataSource = self;
    _mainCollectionView.delegate = self;
    [_mainCollectionView registerClass:[HTImageCollectionViewCell class] forCellWithReuseIdentifier:@"HTImageCollectionViewCell"];
    [self.view addSubview:_mainCollectionView];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HTImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HTImageCollectionViewCell" forIndexPath:indexPath];
    HTImageModel *model = self.dataSourceArray[indexPath.row];
//    cell.backgroundColor = [UIColor redColor];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.photo]];
    cell.imageView.userInteractionEnabled = YES;
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((SCREEN_WIDTH - 6)/3, (SCREEN_WIDTH - 6)/3);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger count = self.dataSourceArray.count;
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:0];
    
    for (int i = 0; i < count; i++) {
        HTImageModel *model = self.dataSourceArray[i];
        MJPhoto *photo = [[MJPhoto alloc]init];
//        NSString *imageUrlStr = [self normolWithString:model.photo_s];
        photo.url = [NSURL URLWithString:model.photo];
    
        HTImageCollectionViewCell *cell = (HTImageCollectionViewCell *)[self.mainCollectionView cellForItemAtIndexPath:indexPath];
        photo.srcImageView = cell.imageView;
        [photos addObject:photo];
    }
    //2.显示放大的图片
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.currentPhotoIndex = indexPath.row;//弹出相册时显示
    //注意这里是装imageview的数组
    browser.photos = photos;//设置所有的图片
    
    [browser show];
}
//判断字符串是否为空
//- (id) normolWithString:(id)string
//{
//    if ([string isKindOfClass:[NSNull class]] || string == nil) {
//        return @"";
//    }
//    return string;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)didClickBackLastViewController{
    [self.navigationController popViewControllerAnimated:nil];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
